package com.example.test_load_pic_rough;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.test_load_pic_rough.adapter.UserListAdapter;
import com.example.test_load_pic_rough.adapter.ViewListAdapter;
import com.example.test_load_pic_rough.bean.User;
import com.example.test_load_pic_rough.listener.OnRecyclerItemClickListener;
import com.example.test_load_pic_rough.listener.RecycleViewOnDragListener;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.xuexiang.xui.adapter.recyclerview.DividerItemDecoration;

import java.util.ArrayList;

public class TestDragActivity extends AppCompatActivity {

    private ArrayList<User> viewList;
    private ArrayList<User> userList;
    private RecycleViewOnDragListener dragListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_drag3);
        dragListener = new RecycleViewOnDragListener();
        initViewListRv();
        initUserListRV();
    }

    //使用宫格layout布局，让宫格的大小随着屏幕的大小而变化
    private void initViewListRv() {
        viewList = new ArrayList<>();
        viewList.add(new User("1", "1"));
        viewList.add(new User("2", "2"));
        viewList.add(new User("3", "3"));
        viewList.add(new User("4", "4"));
        viewList.add(new User("5", "5"));
        viewList.add(new User("6", "6"));

        ViewListAdapter viewListAdapter = new ViewListAdapter(viewList);

        RecyclerView rvViewList = findViewById(R.id.rv_view);


        FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(this);
        //flexDirection 属性决定主轴的方向（即项目的排列方向）。类似 LinearLayout 的 vertical 和 horizontal。
        flexboxLayoutManager.setFlexDirection(FlexDirection.ROW);//主轴为水平方向，起点在左端。
        //flexWrap 默认情况下 Flex 跟 LinearLayout 一样，都是不带换行排列的，但是flexWrap属性可以支持换行排列。
        flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP);//按正常方向换行
//        flexboxLayoutManager.setAlignItems(AlignItems.CENTER);
//        flexboxLayoutManager.setJustifyContent(JustifyContent.CENTER);
        //justifyContent 属性定义了项目在主轴上的对齐方式。
        flexboxLayoutManager.setJustifyContent(JustifyContent.FLEX_START);//交叉轴的起点对齐。

        rvViewList.setLayoutManager(flexboxLayoutManager);
        rvViewList.setAdapter(viewListAdapter);

        //设置item之间的分割线
        rvViewList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                int childAdapterPosition = parent.getChildAdapterPosition(view);//得到每个item在recycleview中的索引
                //通过adapter拿到分为了几行几列
                int columnCount = viewListAdapter.columns;
                //通过上面两个参数可以给非首尾的item添加上分割线

                //现在简化给每个item都添加上分割线
                int dimension = (int) getResources().getDimension(R.dimen.common_decorate);
                outRect.set(dimension, dimension, dimension, dimension);
            }
        });

        //item拖拽的监听
        //设置拖拽的监听
        rvViewList.setOnDragListener(dragListener);
        rvViewList.addOnItemTouchListener(new OnRecyclerItemClickListener(rvViewList) {
            @Override
            public void onItemClick(RecyclerView.ViewHolder vh) {
                //点击的话不做处理
            }

            @Override
            public void onItemLongClick(RecyclerView recyclerView, RecyclerView.ViewHolder vh) {
                //长按之后开始拖拽
                RecycleViewOnDragListener.startDrag(recyclerView, vh);
            }
        });
    }

    private void initUserListRV() {
        userList = new ArrayList<>();
        userList.add(new User("张三", "3333"));
        userList.add(new User("李四", "4444"));
        userList.add(new User("王五", "5555"));
        userList.add(new User("赵六", "6666"));
        userList.add(new User("雨琦", "7777"));
        UserListAdapter userListAdapter = new UserListAdapter(userList);

        RecyclerView rvUserList = findViewById(R.id.rv_user_list);
        rvUserList.setAdapter(userListAdapter);
        rvUserList.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        rvUserList.setLayoutManager(new LinearLayoutManager(this));
        //设置拖拽的监听
        rvUserList.setOnDragListener(dragListener);

        //设置rv中item的触摸事件，用来监听item长按之后的拖拽
        rvUserList.addOnItemTouchListener(new OnRecyclerItemClickListener(rvUserList) {
            @Override
            public void onItemClick(RecyclerView.ViewHolder vh) {
                //单击的时候不做处理
            }

            @Override
            public void onItemLongClick(RecyclerView recyclerView, RecyclerView.ViewHolder vh) {
                //长按之后可以实现拖拽
                //判断viewList是否已经包含了这个对象
                int layoutPosition = vh.getLayoutPosition(); //当前点击的第几个item
                if (viewList.contains(userList.get(layoutPosition))) {
                    showToast("视频窗口中已存在：" + userList.get(layoutPosition).getName());
                    return;
                }
                //如果视频窗口中不包含该对象开始拖拽
                RecycleViewOnDragListener.startDrag(recyclerView, vh);
            }
        });
    }


    private void showToast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }
}